package com.wangnian.wangcai.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.wangnian.wangcai.dao.*;
import com.wangnian.wangcai.service.api.ApiChannel;
import com.wangnian.wangcai.util.Indicators;
import com.wangnian.wangcai.util.TaskUtil;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class BusinessService {
    private final ChiGuDao chiGuDao;
    private final DayInfoDao dayInfoDao;
    private final NameInfoDao nameInfoDao;

    public BusinessService(ChiGuDao chiGuDao, DayInfoDao dayInfoDao, NameInfoDao nameInfoDao) {
        this.chiGuDao = chiGuDao;
        this.dayInfoDao = dayInfoDao;
        this.nameInfoDao = nameInfoDao;
    }

    /**
     * 处理double
     */
    ValueFilter filter = (object, name, value) -> {
        if (value instanceof BigDecimal || value instanceof Double || value instanceof Float) {
            try {
                BigDecimal bigDecimal = new BigDecimal(value.toString()).setScale(3, RoundingMode.HALF_UP);
                return bigDecimal;
            } catch (Exception ex) {
                return 0;
            }
        }
        return value;
    };


    /**
     * 获取持股情况
     *
     * @return
     */
    public List<JSONObject> getChiGuList() {
        List<JSONObject> jsonObjectList = new ArrayList<>();
        List<ChiGu> chiGuList = chiGuDao.findAll(Sort.by("ykZf").descending());
        if (chiGuList.isEmpty()) {
            return jsonObjectList;
        }
        for (ChiGu chiGu : chiGuList) {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(chiGu, filter));
            NameInfo nameInfo = nameInfoDao.findByCode(chiGu.getCode());
            if (nameInfo == null) {
                continue;
            }
            jsonObject.put("zf", String.format("%.3f", nameInfo.getZf()));
            jsonObject.put("close", String.format("%.3f", nameInfo.getPrice()));
            jsonObjectList.add(jsonObject);
        }
        return jsonObjectList;
    }

    /**
     * 大盘MAP信息
     *
     * @return
     */
    public JSONObject dpBuyMap() {
        JSONObject resultJSON = new JSONObject();
        List<JSONObject> jsonObjectList = DPBuyList();
        for (JSONObject jsonObject : jsonObjectList) {
            resultJSON.put(jsonObject.getString("name"), jsonObject.getDouble("value"));
        }
        return resultJSON;
    }

    /**
     * 大盘List信息
     *
     * @return
     */
    public List<JSONObject> DPBuyList() {
        List<JSONObject> resultList = new ArrayList<>();
        //检查大盘是否优秀
        List<DayInfo> dayInfos = dayInfoDao.findByCode("1.000001", Sort.by("time"));
        Indicators.initIndicators(dayInfos);
        DayInfo dayInfo1 = dayInfos.get(dayInfos.size() - 1);
        double zf1 = Indicators.zf(dayInfo1.getHigh(), dayInfo1.getClose());
        double zf2 = Indicators.zf(dayInfo1.getHigh(), dayInfo1.getLow());
        double zf3 = Indicators.zf(dayInfo1.getClose(), dayInfo1.getLow());
        double zf4 = dayInfo1.getZf();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("value", dayInfo1.getClose() + "(" + String.format("%.3f", zf4) + ")" + "-" + dayInfo1.getJ());
        jsonObject.put("name", "当前价");
        resultList.add(jsonObject);

        jsonObject = new JSONObject();
        jsonObject.put("value", dayInfo1.getHigh() + "-" + dayInfo1.getLow());
        jsonObject.put("name", "最高价-最低价");
        resultList.add(jsonObject);

        jsonObject = new JSONObject();
        jsonObject.put("value", String.format("%.3f", zf1));
        jsonObject.put("name", "回落");
        resultList.add(jsonObject);

        jsonObject = new JSONObject();
        jsonObject.put("value", String.format("%.3f", zf3));
        jsonObject.put("name", "上涨");
        resultList.add(jsonObject);

        jsonObject = new JSONObject();
        jsonObject.put("value", ApiChannel.apiService.balance());
        jsonObject.put("name", "可用担保品");
        resultList.add(jsonObject);

        jsonObject = new JSONObject();
        jsonObject.put("value", ApiChannel.apiService.positionProfit());
        jsonObject.put("name", "持仓盈亏");
        resultList.add(jsonObject);

        jsonObject = new JSONObject();
        jsonObject.put("value", nameInfoDao.countByZfGreaterThan(0.0));
        jsonObject.put("name", "涨的个量");
        resultList.add(jsonObject);

        jsonObject = new JSONObject();
        jsonObject.put("value", nameInfoDao.countByZfLessThan(0.0));
        jsonObject.put("name", "跌的个数");
        resultList.add(jsonObject);

        jsonObject = new JSONObject();
        jsonObject.put("value", String.format("%.3f", nameInfoDao.svgZf()));
        jsonObject.put("name", "平均涨幅");
        resultList.add(jsonObject);

        return resultList;
    }

    /**
     * 获取监控的股票
     *
     * @return
     */
    public Set<String> getWatchCodeSet() {
        List<NameInfo> nameInfos = nameInfoDao.findAll();
        return nameInfos.stream().map(NameInfo::getCode).collect(Collectors.toSet());
    }


    /**
     * 获取监控的股票
     *
     * @return
     */
    public List<JSONObject> getNameInfoS() {
        List<NameInfo> nameInfos = nameInfoDao.findByMonitorIsTrue(Sort.by("zf").descending());
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (NameInfo e : nameInfos) {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(e, filter));
            jsonObjectList.add(jsonObject);
        }
        return jsonObjectList;
    }

    /**
     * 获取做T任务
     *
     * @return
     */
    public JSONArray getTRecording() {
        JSONArray recordings = TaskUtil.findAll();
        return recordings;
    }

}
